﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class RateBLL : IBaseInfo
    {
        private static readonly ICalendarGroupDetailDA calendarGroupDetailDA = DataAccess.CreateCalendarGroupDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRateDA rateDA = DataAccess.CreateRateDA();
        private static readonly IRateDetailDA rateDetailDA = DataAccess.CreateRateDetailDA();
        private static readonly ITariffDA tariffDA = DataAccess.CreateTariffDA();

        public RateBLL()
        {
            this.pubParasInfo = null;
        }

        public RateBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public RateBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        private void Copy(string strContractID, RateInfo rateInfo)
        {
            if ((rateInfo.RateDetailInfos != null) && (rateInfo.RateDetailInfos.Count > 0))
            {
                foreach (RateDetailInfo RDI in rateInfo.RateDetailInfos)
                {
                    RDI.RateID = strContractID;
                    RDI.CreatedBy = this.pubParasInfo.UserID;
                    rateDetailDA.InsertRateDetail(RDI, this.dataBaseID[0]);
                }
            }
        }

        public string Delete(List<string> IDS)
        {
            int i;
            string tariffRateIDS = string.Empty;
            string calendarGroupRateIDS = string.Empty;
            foreach (string id in IDS)
            {
                List<TariffInfo> tariffListInfo = tariffDA.GetTariffByByTypeCode("Fixed", id, this.dataBaseID[0]);
                List<CalendarGroupDetailInfo> calendarGroupDetailListInfo = calendarGroupDetailDA.GetCalendarGroupDetailByByRateID(id, this.dataBaseID[0]);
                if ((tariffListInfo != null) && (tariffListInfo.Count > 0))
                {
                    i = 0;
                    while (i < tariffListInfo.Count)
                    {
                        tariffRateIDS = tariffRateIDS + tariffListInfo[i].TariffID + ";";
                        i++;
                    }
                    if (!string.IsNullOrEmpty(tariffRateIDS))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F9190_003", this.pubParasInfo.UserLanguage, new List<string> { tariffRateIDS.Substring(0, tariffRateIDS.Length - 1) });
                    }
                }
                if ((calendarGroupDetailListInfo != null) && (calendarGroupDetailListInfo.Count > 0))
                {
                    for (i = 0; i < calendarGroupDetailListInfo.Count; i++)
                    {
                        object CS00003 = calendarGroupRateIDS;
                        calendarGroupRateIDS = string.Concat(new object[] { CS00003, calendarGroupDetailListInfo[i].CalendarGroupID, "[", calendarGroupDetailListInfo[i].LineID, "];" });
                    }
                    if (!string.IsNullOrEmpty(calendarGroupRateIDS))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F9190_004", this.pubParasInfo.UserLanguage, new List<string> { calendarGroupRateIDS.Substring(0, tariffRateIDS.Length - 1) });
                    }
                }
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<RateInfo> RateInfos = new List<RateInfo>();
                RateInfo rateinfo = null;
                foreach (string id in IDS)
                {
                    rateinfo = rateDA.GetRateByID(dataBase, tran, id, this.dataBaseID[0]);
                    RateInfos.Add(rateinfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                foreach (string id in IDS)
                {
                    rateDetailDA.DeleteRateDetail(dataBase, tran, id, this.dataBaseID[0]);
                }
                if (rateDA.DeleteRate(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<RateInfo>(dataBase, tran, RateInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "RATE", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return rateDA.GetAllRate(this.dataBaseID[0]);
        }

        public object GetInfoByID(string id)
        {
            RateInfo rateInfo = rateDA.GetRateByID(id, this.dataBaseID[0]);
            if (rateInfo != null)
            {
                rateInfo.RateDetailInfos = rateDetailDA.GetRateDetailByID(id, this.dataBaseID[0], this.pubParasInfo);
            }
            return rateInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            RateQueryEntity subjectQueryEntity = obj as RateQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(subjectQueryEntity.RateID))
            {
                sqlWhere.Add(" RATE_ID " + subjectQueryEntity.RateID);
            }
            if (!string.IsNullOrEmpty(subjectQueryEntity.RateName))
            {
                sqlWhere.Add(" RATE_NAME " + subjectQueryEntity.RateName);
            }
            if (!string.IsNullOrEmpty(subjectQueryEntity.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + subjectQueryEntity.IsActive);
            }
            return rateDA.GetRateByQueryList(sqlWhere, subjectQueryEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            RateInfo rateInfo = obj as RateInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                if (rateDA.CheckRateIDUnique(dataBase, tran, rateInfo.RateID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9190_002", this.pubParasInfo.UserLanguage, new List<string> { rateInfo.RateID });
                }
                if (string.IsNullOrEmpty(rateInfo.RateID))
                {
                    rateInfo.RateID = commonMethod.GenNextNumStr(dataBase, tran, "RATE", whLoginID);
                }
                rateInfo.UpdatedBy = rateInfo.CreatedBy;
                rateInfo.CreatedDate = DateTime.Now;
                rateInfo.UpdatedDate = DateTime.Now;
                if (rateDA.InsertRate(dataBase, tran, rateInfo, whLoginID) > 0)
                {
                    strContractID = rateInfo.RateID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "RATE", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            this.Copy(strContractID, rateInfo);
            return strContractID;
        }

        public string Update(object obj)
        {
            RateInfo rateInfo = obj as RateInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                RateInfo oldRateInfo = rateDA.GetRateByID(dataBase, tran, rateInfo.RateID, this.dataBaseID[0]);
                if (rateDA.UpdateRate(dataBase, tran, rateInfo, whLoginID) > 0)
                {
                    strContractID = rateInfo.RateID;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, oldRateInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "RATE", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }
    }
}

